//
//  WeboTabBar.m
//  Weibo
//
//  Created by 千锋 on 15/12/29.
//  Copyright © 2015年 abc. All rights reserved.
//

#import "WeboTabBar.h"
#import "WBTabBarButton.h"

@interface WeboTabBar ()

@property(nonatomic,strong) NSMutableArray * buttonArray;

// 当前选中按钮
@property (nonatomic,strong) WBTabBarButton * selecteBtn;

// 中间加号按钮
@property(nonatomic,strong) UIButton * plusBtn;

@end


@implementation WeboTabBar

- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        // 创建一个按钮 设置一次醒的属性
        UIButton * plusBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [self addSubview:plusBtn];
        [plusBtn setImage:[UIImage imageNamed:@"tabbar_compose_icon_add"] forState:UIControlStateNormal];
        [plusBtn setImage:[UIImage imageNamed:@"tabbar_compose_icon_add_highlighted"] forState:UIControlStateHighlighted];
        [plusBtn setBackgroundImage:[UIImage imageNamed:@"tabbar_compose_button"] forState:UIControlStateNormal];
        [plusBtn setBackgroundImage:[UIImage imageNamed:@"tabbar_compose_button_highlighted"] forState:UIControlStateHighlighted];
        self.plusBtn = plusBtn;
        [plusBtn addTarget:self action:@selector(plusBtnPressd) forControlEvents:UIControlEventTouchUpInside];
    }
    return self;
}
-(void)plusBtnPressd{
    if (_plubBlock) {
        _plubBlock();
    }
}
-(NSMutableArray *)buttonArray
{
    if (_buttonArray == nil) {
        _buttonArray = @[].mutableCopy;
    }
    return _buttonArray;
}


- (void)setTabBarItem:(UITabBarItem *)tabBarItem
{
    // 获取到 标题 title，图片 image
//    NSLog(@"tabBarItem title = %@",tabBarItem.title);
    
    //每次进来创建一个button
    WBTabBarButton *button = [WBTabBarButton buttonWithType:UIButtonTypeCustom];
    button.tabBarItem = tabBarItem;
    [self addSubview:button];
    [button addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchDown];
    
    // 存储在全局按钮数组里面
    [self.buttonArray addObject:button];
    button.ratio = 0.7;
    
    // 当第一次调用这个方法，就让第一个按钮处于选中状态
    if (self.buttonArray.count == 1) {
        [self buttonPressed:button];
    }
    
    
//    button.imageEdgeInsets = UIEdgeInsetsMake(0, 45, 20, 10);
//    button.titleEdgeInsets = UIEdgeInsetsMake(35, 10, 5, 10);
    
}
-(void)layoutSubviews
{
    [super layoutSubviews];
    //布局子视图
    // 循环按钮数组，做相应的布局
    CGFloat btnW = CGRectGetWidth(self.frame)/(CGFloat)(self.buttonArray.count+1);
    
//    self.plusBtn.frame = CGRectMake(bX*3, bY, bW, bH);
    CGFloat plusW = self.plusBtn.currentBackgroundImage.size.width;//btnW;
    CGFloat plusH = self.plusBtn.currentBackgroundImage.size.height;//CGRectGetHeight(self.frame);
    self.plusBtn.frame = CGRectMake(0, 0, plusW, plusH);
    self.plusBtn.center = CGPointMake(CGRectGetWidth(self.frame)*0.5, CGRectGetHeight(self.frame)*0.5);
    
    for (int index = 0; index < self.buttonArray.count; index++) {
        
        WBTabBarButton *btn = self.buttonArray[index];
        CGFloat bX = btnW * index;
        if (index > 1) {
            bX += btnW;
        }
        CGFloat bY = 0;
        CGFloat bW = btnW;
        
        CGFloat bH = CGRectGetHeight(self.frame);
        
        btn.frame = CGRectMake(bX, bY, bW, bH);
        
    }
}

#pragma mark - 按钮点击
- (void)buttonPressed:(WBTabBarButton *)btn{
    
#if 0
    btn.selected = !btn.selected;
    //1. 循环
    for (WBTabBarButton *button in self.buttonArray) {
        button.selected = NO;
    }
    btn.selected = YES;
#endif
    //2 记录
    _selecteBtn.selected = NO;
    btn.selected = YES;
    _selecteBtn = btn;
    
    NSInteger index = [self.buttonArray indexOfObject:btn];
#if 0
    // 响应代理方法 , 代理判断[_delegate respondsToSelector:@selector(passIndex:)]是否可以相应方法
    if ([_delegate respondsToSelector:@selector(passIndex:)]) {
        [_delegate passIndex:index];
    }
#endif
    if (_passIndex) {
        _passIndex(index);
    }

    
}
@end
